package ml.coursedesign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ml.coursedesign.common.lang.Result;
import ml.coursedesign.entity.SignVo;
import ml.coursedesign.entity.StudentSign;
import ml.coursedesign.entity.TeacherSign;
import ml.coursedesign.mapper.TeacherSignMapper;
import ml.coursedesign.service.TeacherSignService;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TeacherSignServiceImpl extends ServiceImpl<TeacherSignMapper, TeacherSign>  implements TeacherSignService {
    @Override
    public List<TeacherSign> lookSignHistory(String classid, String date) {
        QueryWrapper<TeacherSign> signQueryWrapper = new QueryWrapper<>();
        signQueryWrapper.eq("course_no",classid);
        if(date!=null){
            signQueryWrapper.like("sign_time",date);
        }
        List<TeacherSign> teacherSigns = baseMapper.selectList(signQueryWrapper);
        return teacherSigns;

    }

    @Override
    public boolean releaseSign(String classid, String teacherId, SignVo teacherSign) {
        System.err.println(teacherSign);

        TeacherSign teacherSign1 = new TeacherSign();
        teacherSign1.setCourseNo(classid);
        teacherSign1.setTecId(teacherId);
        Date startTime = teacherSign.getSignTime();
        Date endTime =teacherSign.getSignbackTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        teacherSign1.setSignTime(sdf.format(startTime));
        teacherSign1.setSignTimeRange(teacherSign.getSignTimeRange());


        teacherSign1.setSignbackTime(sdf.format(endTime));
        teacherSign1.setSingbackTimeRange(teacherSign.getSingbackTimeRange());
        int insert = baseMapper.insert(teacherSign1);
        if(insert>0){
            return true;
        }
        return false;

    }

    @Override
    public Boolean isSign(String classid) {
        Date date = new Date();
        long time = date.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(date.toString().substring(0,17));
        System.out.println(sdf.format(date).substring(0,17));
        QueryWrapper<TeacherSign> teacherSignQueryWrapper = new QueryWrapper<>();
        teacherSignQueryWrapper.eq("course_no",classid);
        teacherSignQueryWrapper.ge("sign_time",sdf.format(date).substring(0,17));
        TeacherSign teacherSign = baseMapper.selectOne(teacherSignQueryWrapper);
        String range= teacherSign.getSignTimeRange();


        try {
            Date signDate = sdf.parse(teacherSign.getSignTime());
            long min = (date.getTime() -signDate.getTime())/(1000 * 60 );
            if(min<Integer.parseInt(range)){
                return true;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
      return false;
    }

    @Override
    public Boolean isSignBack(String classId) {
        Date date = new Date();
        long time = date.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(date.toString().substring(0,17));
        System.out.println(sdf.format(date).substring(0,17));
        QueryWrapper<TeacherSign> teacherSignQueryWrapper = new QueryWrapper<>();
        teacherSignQueryWrapper.eq("course_no",classId);
        teacherSignQueryWrapper.ge("sign_time",sdf.format(date).substring(0,17));
        TeacherSign teacherSign = baseMapper.selectOne(teacherSignQueryWrapper);
        String range= teacherSign.getSingbackTimeRange();


        try {
            Date signBackDate = sdf.parse(teacherSign.getSignbackTime());
            long min = (date.getTime() -signBackDate.getTime())/(1000 * 60 );
            if(min<Integer.parseInt(range)){
                return true;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
